﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Collections.Generic;
using Ccit.Utils;
using Ccit.OA.MsPersonDoc;
using Newtonsoft.Json;

namespace Ccit.OA.PersonDoc
{
    public partial class persondoc_mydoc_list : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                //当前文件夹ID
                hidFoldID.Value = (null != Request.QueryString["folderID"]) ? Request.QueryString["folderID"].ToString() : "0";
                //所属类别（我的文档,我的共享文档,他人共享文档）
                hidfolderType.Value = (null != Request.QueryString["folderType"]) ? Request.QueryString["folderType"].ToString() : "0";
                hidShareType.Value = (null != Request.QueryString["Share"]) ? Request.QueryString["Share"].ToString() : "00";
                hidUserId.Value = (null != Request.QueryString["UserIdShare"]) ? Request.QueryString["UserIdShare"].ToString() : "";
                InitPage();
            }
            
            BindGridView();
        }

        private void InitPage()
        {
            string sWhere,sSpace;
            double sSum = 0.00;
            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;

            string userId = hidUserId.Value;
            /*用Session保存 文件夹ID号*/
           // hidFoldID.Value = (null != Session["folderId"]) ? Session["folderId"].ToString().Trim() : "1";
            Session["folderID"] = hidFoldID.Value.Trim();
            /*文件夹所属用户  ""为当前人自己,非空时为共享人*/
            if (hidUserId.Value.Trim() == "")
            {
                hidUserId.Value = loginUser.UserId;
            }
            Session["UserIdShare"] = hidUserId.Value;

            PersonDocBLL Bll = new PersonDocBLL();
            sWhere = " and userId = " + loginUser.UserId;
            DataSet dSet = Bll.GetPersonDocSpaceMess(sWhere);
            #region 获取系统空间
            ConfigLib oConfig = new ConfigLib();
            string PERSONALSPACE = oConfig.GetAttribute("CCIT_SYSTEM/CCIT_PERSONALSPACE", eValueType.TEXT);
            #endregion
            if (dSet.Tables[0].Rows.Count != 0)
            {
                sSpace = "个人文档空间";
                if (dSet.Tables[0].Rows[0]["Archives"].ToString().Trim() == "0")
                {
                    if (PERSONALSPACE.Trim() == "") { PERSONALSPACE = "0"; }
                    sSum = double.Parse(PERSONALSPACE.Trim());
                    sSpace += PERSONALSPACE;
                }
                else
                {
                    sSum = double.Parse(dSet.Tables[0].Rows[0]["Archives"].ToString().Trim());
                    sSpace += dSet.Tables[0].Rows[0]["Archives"].ToString().Trim();
                }
                sSpace += "M,已使用";
                sSpace += dSet.Tables[0].Rows[0]["Already"].ToString().Trim();
                sSpace += "M,剩余空间";
                hidsySpace.Value = (sSum - double.Parse(dSet.Tables[0].Rows[0]["Already"].ToString().Trim())).ToString();
                sSpace += hidsySpace.Value + "M";
            }
            else {
                sSpace = "数据有误,系统未找到当前人的空间设置";
                hidsySpace.Value = "0";
            }

            this.lbSpace.Text = sSpace;
        }

        private void BindGridView()
        {
            dgMain.DataSource = GetDateBind();
            dgMain.DataBind();
        }

        private DataSet GetDateBind()
        {
            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;
            string sQuery = string.Empty;
            string sType = hidfolderType.Value.Trim();
           
  
            PersonDocBLL bll = new PersonDocBLL();
            MsFiles Ms = new MsFiles();

            Ms.folderId = hidFoldID.Value.Trim();
            Ms.userId = hidUserId.Value;
            Ms.jobId = loginUser.PosId;
            Ms.levelId = loginUser.ClassId;
            Ms.deptId = loginUser.DeptId;
            Ms.agencId = loginUser.OrganId;

            DataSet dSet = bll.GetFilesMess(Ms, sType);
            bll.Release();
            bll = null;
            return dSet;
        }

        protected void dgMain_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
        {
            if (e.RowType != DevExpress.Web.ASPxGridView.GridViewRowType.Data) return;
            #region 更新列值
            PersonDocBLL bll = new PersonDocBLL();
            string ID = dgMain.GetRowValues(e.VisibleIndex, "ID").ToString().Trim();
            
            string fileTitle = dgMain.GetRowValues(e.VisibleIndex, "Title").ToString().Trim();
            string fileType = dgMain.GetRowValues(e.VisibleIndex, "FileType").ToString().Trim();
            string fileLength = dgMain.GetRowValues(e.VisibleIndex, "Length").ToString().Trim();
            //fileTitle = "<img src='../images/toolbar/" + bll.GetTypeimage(fileType) + "' border='0'>" + fileTitle;
            e.Row.Cells[1].Text = "<img src='../images/filetype/" + bll.GetTypeimage(fileType) + "' border='0'>";
            e.Row.Cells[2].Text = fileTitle;
            if (fileLength != "")
            {
                e.Row.Cells[3].Text = (double.Parse(fileLength)/1024).ToString("F2").Trim() + "KB";
            }
            //e.Row.Cells[4].Text = bll.GetTypetxt(fileType);
            #endregion

            #region 操作列按钮
            LinkButton lnbEdit = (LinkButton)dgMain.FindRowCellTemplateControl(e.VisibleIndex, null, "lnbEdit");
            LinkButton lnbDel = (LinkButton)this.dgMain.FindRowCellTemplateControl(e.VisibleIndex, null, "lnbDel");

            string encodeparam = string.Empty;
            if (fileType == "")
            {
                encodeparam = Base64Lib.Encode("folderID=" + ID + "&folderType=" + hidfolderType.Value.Trim() + "&Share=" + hidShareType.Value + "&UserIdShare=" +hidUserId.Value.Trim());
                e.Row.Cells[2].Text = "<a href=\"javascript:doLocationFolder('" + encodeparam + "');\">" + fileTitle + "</a>";
                lnbDel.Visible = false;
            }
            else
            {
                encodeparam = Base64Lib.Encode("Field=" + ID+"&folderType="+hidfolderType.Value.Trim()+"&UserIdShare=" +hidUserId.Value.Trim());
                 lnbEdit.Visible = false;
                 e.Row.Cells[2].Text = "<a href=\"javascript:OpenUpLoadPage('" + encodeparam + "');\">" + fileTitle + "</a>";
            }

            if (hidShareType.Value != "00")
            {
                lnbEdit.Enabled = false;
                lnbDel.Enabled = false;
            }
            else
            {

                lnbDel.Attributes["onclick"] = "javascript:doDelete('" + ID + "','" + fileType + "');return false;";
                lnbEdit.Attributes["onclick"] = "javascript:return doEditFolder('" + Base64Lib.Encode("type=1&FId=" + ID) + "');";
            }
            #endregion

            bll.Release();
            bll = null;
        }

        /// <summary>
        /// 删除ajax方法 删除文件
        /// </summary>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DeleteFile(string serExpr, string operType)
        {
            MsFiles ms = (MsFiles)JavaScriptConvert.DeserializeObject(serExpr, typeof(MsFiles));
            MsFolder msfolder = new MsFolder();
            PersonDocBLL bll = new PersonDocBLL();
            msfolder = bll.DelFiles(ms);
            var newObj = new { operType = operType, resultSign = "1", resultDelId = msfolder.FId, resultTypeId = ms.fileType };
            string resultSerExpr = JavaScriptConvert.SerializeObject(newObj);

            bll.Release();
            bll = null;
            ms = null;
            return resultSerExpr;
        }

        /// <summary>
        /// 粘贴ajax方法 粘贴文件
        /// </summary>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string ShearFile(string serExpr, string operType,string sWork)
        {
            bool bFlag = false;
            string resultlossName = "";
            string resultSuccessID = "";   //成功文件夹ID
            string resultSuccessType = ""; //成功文件夹类型
            string[] resultList;
            MsFiles ms = (MsFiles)JavaScriptConvert.DeserializeObject(serExpr, typeof(MsFiles));
            PersonDocBLL bll = new PersonDocBLL();
            List<string> lstResult =  bll.Fiesmove(ms.Id, sWork, ms.fileType, ms.folderId,ms.userId);
            if (lstResult.Count != 0)
            {
                for (int i = 0; i < lstResult.Count; i++)
                {
                    resultList = lstResult[i].Split(';');
                    if (resultList[2] == "-999")
                    {
                        resultlossName += resultList[1] + ",";
                    }
                    else {
                        resultSuccessID += resultList[0] + ",";
                        if(i != lstResult.Count - 1)
                        {
                            resultSuccessType += ",";
                        }
                        
                    }
                }
            }
            var newObj = new { operType = operType, resultSign = "1", resulType = sWork, resultlossName = resultlossName.Trim(','), resultSuccessID = resultSuccessID.Trim(','), resultSuccessType = resultSuccessType };
            string resultSerExpr = JavaScriptConvert.SerializeObject(newObj);

            bll.Release();
            bll = null;
            ms = null;
            return resultSerExpr;
        }
    }
}
